﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFIDA.U9.SM.Ship;
using UFIDA.U9.PM.PO;
using UFSoft.UBF.PL;
using UFIDA.U9.PM.Rcv;
using UFIDA.U9.SM.RMA;
using UFIDA.U9.SM.SO;
using UFIDA.U9.CBO.SCM.Customer;
using UFIDA.U9.PR.PurchaseRequest;
using UFIDA.U9.AP.Payment;
using UFIDA.U9.CBO.FI.Enums;
using UFSoft.UBF.Util.DataAccess;
using System.Data;
using UFIDA.U9.AP.APBill;
using UFIDA.U9.AP.Enums;
using UFSoft.UBF.PL.Engine;


namespace UFIDA.U9.YD.Cli.PlugBE
{
    /// <summary>
    /// 运达项目
    /// 请款单
    /// 保存时,单据类型设置
    /// 解决老魏之前的BUG
    /// </summary>
    class YDPayReqFundHeadExtend : IEventSubscriber
    {
        //配置XX.sub.xml文件放置到portal/bin下，dll文件放置到portal/ApplicationServer/Libs下
        public void Notify(params object[] args)
        {
            if (args != null && args.Length != 0 && args[0] is EntityEvent)
            {
                BusinessEntity.EntityKey key = ((EntityEvent)args[0]).EntityKey;
                PayReqFundHead be = key.GetEntity() as PayReqFundHead;
                if (be == null)
                    return;

                //判断组织生效
                if (be.Org.Code == "123"
                    ||be.Org.Code=="123")
                {
                    //不为新增时,直接跳过
                    if(be.SysState!= ObjectState.Inserted) return;

                    //通过请款单查询应付单or采购订单,查询付款计划(客开实体),获取 付款类别

                    //请款明细行
                    if (be.PayReqFundUses == null || be.PayReqFundUses.Count == 0) return;
                    PayReqFundUse useline = be.PayReqFundUses[0];
                    //请款用途行
                    if (useline.PayReqFundDetails == null || useline.PayReqFundDetails.Count == 0) return;
                    PayReqFundDetail subline = useline.PayReqFundDetails[0];
                    //依据请款用途,获取来源单号
                    string srcdocno = "";
                    if(subline.SrcBillType== PayReqFundSrcDocTypeEnum.PO)
                    {
                        //预付款,来源=PO
                        PurchaseOrder bill= PurchaseOrder.Finder.FindByID(subline.SrcBillID);
                        if(bill==null) return;
                        srcdocno = bill.DocNo;
                    }
                    else if(subline.SrcBillType == PayReqFundSrcDocTypeEnum.APBill)
                    {
                        //标准,来源=应付
                        APBillHead bill = APBillHead.Finder.FindByID(subline.SrcBillID);
                        if (bill == null) return;
                        srcdocno = bill.DocNo;
                    }
                    else
                    {
                        return;
                    }
                    

                    //查询付款计划(客开表),获取 付款类别
                    string sql = $@"select line.APDocNo,line.PayReqFundHead,line.PayType
                                    from CusDev_AP_PayReqFundDetail line
                                    where 1=1
	                                    and line.APDocNo='{srcdocno}' or PODocNo='{srcdocno}'";
                    DataSet ds;
                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null,out ds);
                    if(ds==null||ds.Tables==null||ds.Tables.Count==0) 
                        throw new Exception($"依据应付单查询付款计划为空, SQL:{sql}");

                    //依据 付款类别 判断 请款单 单据类型
                    //付款类别 0-材料类 1-费用类 2-材料类分批
                    string qkyt = ds.Tables[0].Rows[0]["PayType"].ToString();
                    string djlx="";
                    if (qkyt=="1")
                    {
                        djlx = "002";//费用类请款

                        //更新请款 单据类型
                        string path = $"Code='{djlx}' and Org={be.OrgKey.ID}";
                        PayReqFundDocType dt = PayReqFundDocType.Finder.Find(path);
                        if (dt == null) throw new Exception($"查询请款单类型出错, path:[{path}]");
                        be.DocumentType = dt;
                    }
                }
            }
        }
    }
}
